var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({
    data: {
        list_title: [
            { title: '综合', select: 0 },
            { title: '最新', select: 1 },
            { title: '价格', select: 2 },
            { title: '销量', select: 3 },
        ],
        goodsList: [],
        list_tab: 0,
        catId: 0,
        order: 1,
        keyword: "",
        Sorting: true,
        img_url: c.img_url,
        Sorting1: true,
        page: 1
    },
    onLoad: function (options) {
        if (options.id) {
            this.setData({
                catId: options.id
            })
        }
        if (options.keyword) {
            this.setData({
                keyword: options.keyword
            });
        }
        this.getGoods();

    },
    //下拉刷新
    onPullDownRefresh: function () {
        this.setData({
            goodsList: [],
            page: 1,
        });
        this.getGoods();
    },
    //上拉加载
    onReachBottom: function () {
        this.getGoods();
    },
    getGoods: function () {
        var that = this;
        var catId = that.data.catId;
        var order = that.data.order;
        var keyword = that.data.keyword;
        var data = {
            url: "/wsgoods/goods/lists",
            page: that.data.page,
            limit: 10,
            cat_id: catId,
            keyword: keyword,
            order: order,
            rec_id:""
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                if (data.length > 0) {
                    that.setData({
                        page: that.data.page + 1
                    });
                }
                for (var index in data) {
                    data[index].pic = c.img_url + data[index].pic;
                  var points_price = data[index]['price'] - data[index]['point'];
                  data[index]['points_price'] = points_price.toFixed(2);
                }
                if(that.data.page == 1){
                    that.setData({
                        goodsList: data
                    });
                }else{
                    that.setData({
                        goodsList: that.data.goodsList.concat(data)
                    });
                }
                
                
            } else {

            }
            wx.stopPullDownRefresh();
        });
    },
  jump: function (res) {
    utils.r("/pages/shop/goodsdetail/index?id=" + res.currentTarget.dataset.id);
  },
    list_tab: function (e) {
        let key = e.currentTarget.dataset.select;
        this.setData({
            list_tab: key,
            page:1
        });
        if (key == 0) {//综合
            this.setData({
                order: 1
            })
        } else if (key == 1) {//最新
            this.setData({
                order: 7
            })
        } else if (key == 2) {//价格排序
            this.setData({
                Sorting1: false,
                Sorting: !this.data.Sorting
            });
            if (!this.data.Sorting) {
                this.setData({
                    order: 5
                })
            } else {
                this.setData({
                    order: 6
                })
            }
        } else {
            this.setData({
                order: 2
            })
        }
        this.getGoods();
    }
})
